package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ClockRule;
import com.ruoyi.system.service.IClockRuleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * 考勤规则Controller
 * 
 * @author ruoyi
 * @date 2021-03-10
 */
@Controller
@RequestMapping("/system/rule")
public class ClockRuleController extends BaseController
{
    private String prefix = "system/rule";

    @Autowired
    private IClockRuleService clockRuleService;

    /**
     * 新增考勤规则
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }



    @RequiresPermissions("system:rule:view")
    @GetMapping()
    public String view(ClockRule reqClockRule, ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        reqClockRule.setTenantId(sysUser.getUserId());
        reqClockRule.setSuitYear(DateUtils.getCurentYear());
        ClockRule clockRule = clockRuleService.selectClockRuleByYearAndTenantId(reqClockRule);
        mmap.put("clockRule", clockRule);
        return prefix + "/edit";
    }

    /**
     * 修改保存考勤规则
     */
    @RequiresPermissions("system:rule:edit")
    @Log(title = "考勤规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ClockRule clockRule)
    {
        return toAjax(clockRuleService.updateClockRule(clockRule));
    }
}
